package tron.config;

import lombok.SneakyThrows;
import org.springframework.core.convert.converter.Converter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateConverter implements Converter<String, Date> {
    @SneakyThrows
    @Override
    public Date convert(String source) {
        System.out.println("======Date convert======"+source);
        switch (source.length()) {
            case 23:
                //0000-00-00T00:00:00.000  处理LocalDateTime对象转换
                return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").parse(source);
            case 19:
                //0000-00-00 00:00:00  处理String对象转换
                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(source);
            case 28:
                //Wed Feb 00 00:00:00 CST 0000  处理Date对象转换
                return new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US).parse(source);
            default:
                throw new RuntimeException(String.format("时间解析异常,不能将参数值%1$s解析为Date", source));
        }
    }
}
